package graphic;

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneLayout;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import utility.Utility;

public class LogPanel extends JScrollPane {

	protected static LogPanel LOGPANEL;
	
	public static String SERVER = "Server";
	
	StyledDocument logDocumentStyle;
	Style userIdMessageStyle;
	Style logMessageStyle;
	
	public LogPanel()
	{
		super();
		super.setLayout(new ScrollPaneLayout());
		
		JTextPane log = new JTextPane();
		log.setEditable(false);
		log.setBorder(BorderFactory.createEmptyBorder(10, 10, 30, 10));
		
		this.logDocumentStyle = log.getStyledDocument();
		
	    Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE );
	    Style welcomeMessage = this.logDocumentStyle.addStyle( "welcome", def );
	    StyleConstants.setSuperscript( welcomeMessage, true );
	    StyleConstants.setForeground(welcomeMessage, Color.black);
	    StyleConstants.setFontSize( welcomeMessage, 20 );
	    StyleConstants.setBold( welcomeMessage, true );
		try {
			this.logDocumentStyle.insertString( 0, "Server Started at " + Utility.getTimestamp() + "\n", welcomeMessage );
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		
		super.getViewport().add(log);
		
		this.initLogStyles();		
	}
	
	/**
	 * Writes a new messages into the chat area
	 * @param userId the player who sent the message
	 * @param msg the chat message
	 */
	public void writeLogMessage(String userId, String msg, String fileName)
	{   
		try {
			this.logDocumentStyle.insertString( this.logDocumentStyle.getLength(), "[" + Utility.getTimestamp() + "] " + userId + ": ", this.userIdMessageStyle );
			this.logDocumentStyle.insertString( this.logDocumentStyle.getLength(), msg + " " + fileName + "\n", this.logMessageStyle );
			
			JScrollBar scrollBar = super.getVerticalScrollBar();
			scrollBar.setValue(scrollBar.getMaximum());
			
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Initializes the styles of the chat area
	 */
	public void initLogStyles()
	{
		Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE );
		
	    this.logMessageStyle = this.logDocumentStyle.addStyle( "chatMessage", def );
	    StyleConstants.setSuperscript( this.logMessageStyle, true );
	    StyleConstants.setFontFamily(this.logMessageStyle, "Comic Sans MS");
	    StyleConstants.setFontSize( this.logMessageStyle, 15 );
	    StyleConstants.setForeground(this.logMessageStyle, Color.black);
	    StyleConstants.setBold( this.logMessageStyle, false );
	    
	    this.userIdMessageStyle = this.logDocumentStyle.addStyle( "playerChatMessageStyle", def );
	    StyleConstants.setSuperscript( this.userIdMessageStyle, true );
	    StyleConstants.setFontFamily(this.userIdMessageStyle, "Comic Sans MS");
	    StyleConstants.setFontSize( this.userIdMessageStyle, 15 );
	    StyleConstants.setForeground(this.userIdMessageStyle, Color.black);
	    StyleConstants.setBold( this.userIdMessageStyle, true );
	}
	
	public static synchronized LogPanel getInstance()
	{
		if(LOGPANEL == null) LOGPANEL = new LogPanel();
		
		return LOGPANEL;
	}
	
}
